
export default class WallCorrugationsMaterialProperty {
  constructor(options) {
    this._definitionChanged = new Cesium.Event();
    this._color = undefined;
    this.color = options.color;
    this.duration = options.duration;
    this.trailImage = options.trailImage;
    this._time = new Date().getTime();
  }

  get isConstant() {
    return false;
  }

  get definitionChanged() {
    return this._definitionChanged;
  }

  getType(time) {
    return Cesium.Material.WallCorrugationsMaterialType;
  }

  getValue(time, result) {
    if (!Cesium.defined(result)) {
      result = {};
    }

    result.color = Cesium.Property.getValueOrDefault(
      this._color,
      time,
      Cesium.Color.WHITE,
      result.color
    );
    result.time =
      ((new Date().getTime() - this._time) % this.duration) / this.duration;
    if (this.trailImage) {
      result.image = this.trailImage;
    } else {
      result.image = Cesium.Material.WallCorrugationsMaterialImage;
    }
    return result;
  }

  equals(other) {
    return (
      this === other ||
      (other instanceof WallCorrugationsMaterialProperty &&
        Cesium.Property.equals(this._color, other._color))
    );
  }
}

Object.defineProperties(WallCorrugationsMaterialProperty.prototype, {
  color: Cesium.createPropertyDescriptor("color"),
});

Cesium.WallCorrugationsMaterialProperty = WallCorrugationsMaterialProperty;
Cesium.Material.WallCorrugationsMaterialProperty =
  "WallCorrugationsMaterialProperty";
Cesium.Material.WallCorrugationsMaterialType = "WallCorrugationsMaterialType";
Cesium.Material.WallCorrugationsMaterialImage =
  "";
Cesium.Material.WallCorrugationsMaterialSource =
  "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
{\n\
czm_material material = czm_getDefaultMaterial(materialInput);\n\
vec2 st = materialInput.st;\n\
vec4 colorImage = texture(image, vec2(fract(st.t - time), st.t));\n\
vec4 fragColor;\n\
fragColor.rgb = color.rgb / 1.0;\n\
fragColor = czm_gammaCorrect(fragColor);\n\
material.alpha = colorImage.a * color.a;\n\
material.diffuse = color.rgb;\n\
material.emission = fragColor.rgb;\n\
return material;\n\
}";

Cesium.Material._materialCache.addMaterial(
  Cesium.Material.WallCorrugationsMaterialType,
  {
    fabric: {
      type: Cesium.Material.WallCorrugationsMaterialType,
      uniforms: {
        color: new Cesium.Color(0.0, 0.0, 0.0, 1.0),
        image: Cesium.Material.WallCorrugationsMaterialImage,
        time: 0,
      },
      source: Cesium.Material.WallCorrugationsMaterialSource,
    },
    translucent: function (material) {
      return true;
    },
  }
);
